package com.zrrd.yunchmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.yunchmall.product.entity.ProductCategory;
import com.zrrd.yunchmall.product.mapper.ProductCategoryMapper;
import com.zrrd.yunchmall.product.service.IProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    @Override
    public List<ProductCategory> listWithChildren() {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        queryWrapper.or();
        queryWrapper.eq("level",0);
        List<ProductCategory>categoryList=super.list(queryWrapper);
        categoryList.forEach(productCategory -> {
            queryWrapper.clear();
            queryWrapper.eq("parent_id",productCategory.getId());
            productCategory.setChildren(super.list(queryWrapper));
        });
        return categoryList;
    }
}
